import 'package:injectable/injectable.dart';
import 'package:sonora/bloc/base/sj_base_bloc.dart';
import 'package:sonora/bloc/onboarding/sj_onboarding_event.dart';
import 'package:sonora/bloc/onboarding/sj_onboarding_state.dart';
import 'package:sonora/utils/sj_storage_utils.dart';

@Injectable()
class SjOnboardingBloc
    extends SjBaseBloc<SjOnboardingEvent, SjOnboardingState> {
  SjOnboardingBloc() : super(SjOnboardingState.initial()) {
    on<PageChanged>((event, emit) {
      emit(
          state.copyWith(currentPage: event.page, isLastPage: event.page == 2));
    });

    on<NextPage>((event, emit) {
      final next = state.currentPage + 1;
      emit(state.copyWith(currentPage: next, isLastPage: next == 2));
    });

    on<PreviousPage>((event, emit) {
      final previous = state.currentPage > 0 ? state.currentPage - 1 : 0;
      emit(state.copyWith(currentPage: previous, isLastPage: previous == 2));
    });

    on<StartApp>((event, emit) {});
    on<InitialPage>((event, emit) {
      SjStorageUtils.saveAppFirstLaunch();
    });
  }
}
